package rs.belex.belexdroid.services.impl.ksoap2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import rs.belex.belexdroid.services.interfaces.IServiceClient;
import android.util.Log;

public abstract class ServiceClientKsoap2Impl implements IServiceClient {

	private static String TAG = LoginKsoap2Impl.class.getSimpleName();
	protected String NAMESPACE = "http://www.belex.rs/belexdroid";
	protected static String URL = "http://94.189.165.158:8080/StockAxis/services/ServicesImpl";

	public SoapObject requestComplexType(SoapObject request, String soapAction) {
		Log.d(TAG, "Requesting SoapObject from operation: " + request.getName()
				+ "() that returns completx type");
		SoapObject complexObject = null;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		try {
			androidHttpTransport.call(soapAction, envelope);

			// Get the SAOP Envelope back and the extract the body
			SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

			// Requesting property "METHOD_NAME+Return" e.g. "getMessageReturn"
			// in order to get complex type of object from envelope
			complexObject = (SoapObject) resultsRequestSOAP.getProperty(request
					.getName()
					+ "Return");
		} catch (Exception E) {
			Log.e(TAG, "Error while working with soap envelope response");
			E.printStackTrace();
		}
		return complexObject;

	}

	public List<SoapObject> requestComplexTypeList(SoapObject request,
			String soapAction) {
		Log.d(TAG, "Requesting SoapObject from method: " + request.getName()
				+ " that returns completx type");
		List<SoapObject> complexObjects = null;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		try {
			androidHttpTransport.call(soapAction, envelope);

			// Get the SAOP Envelope back and the extract the body
			SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
			int propsNum = resultsRequestSOAP.getPropertyCount();
			complexObjects = new LinkedList<SoapObject>();
			// Iterate through properties and make the list
			for (int i = 0; i < propsNum; i++) {
				SoapObject element = (SoapObject) resultsRequestSOAP
						.getProperty(i);
				complexObjects.add(element);
			}
		} catch (Exception E) {
			Log.e(TAG, "Error while working with soap envelope response");
			E.printStackTrace();
		}
		return complexObjects;
	}

	/**Parser implementation*/
	Date dateParser(String dateResponse){
		//Unsuccessfully tried to parse date response with yyyy-MM-dd'T'HH:mm:ss.SSSZ
		//there are solutions with substrings like below
		//representation of calendar date (yyyy-MM-dd)
		String calendar = dateResponse.substring(0, 10);
		//representation of time (HH:mm:ss)
		String time = dateResponse.substring(11, 19);
		
		SimpleDateFormat format = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(calendar + " " + time);
		} catch (ParseException e) {
			Log.e(TAG, "Error while parsing date with SimpleDateFormat");
			e.printStackTrace();
		}
		//Because ksoap changes time zone this simple config. is needed
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, 1);
		date = cal.getTime();
		
		return date;
	}
}
